package com.ruoyi.web.controller.course;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.SysConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.SysPaper;
import com.ruoyi.course.service.ISysPaperService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷Controller
 * 
 * @author YJF
 */
@RestController
@RequestMapping("/course/paper")
public class SysPaperController extends BaseController
{
    @Autowired
    private ISysPaperService sysPaperService;

    /**
     * 查询试卷列表
     */
    @PreAuthorize("@ss.hasPermi('course:paper:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPaper sysPaper)
    {
        startPage();
        List<SysPaper> list = sysPaperService.selectSysPaperList(sysPaper);
        return getDataTable(list);
    }

    /**
     * 查询全部试卷列表
     */
    @PreAuthorize("@ss.hasPermi('course:paper:list')")
    @GetMapping("/wholePaper")
    public AjaxResult wholePaper(SysPaper sysPaper)
    {
        return AjaxResult.success(sysPaperService.selectSysPaperList(sysPaper));
    }

    /**
     * 导出试卷列表
     */
    @PreAuthorize("@ss.hasPermi('course:paper:export')")
    @Log(title = "试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPaper sysPaper)
    {
        List<SysPaper> list = sysPaperService.selectSysPaperList(sysPaper);
        ExcelUtil<SysPaper> util = new ExcelUtil<SysPaper>(SysPaper.class);
        util.exportExcel(response, list, "试卷数据");
    }

    /**
     * 获取试卷详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:paper:query')")
    @GetMapping(value = "/{paperId}")
    public AjaxResult getInfo(@PathVariable("paperId") Long paperId)
    {
        SysPaper sysPaper = new SysPaper();
        sysPaper.setPaperId(paperId);
        return AjaxResult.success(sysPaperService.selectSysPaperByPaperId(sysPaper));
    }

    /**
     * 新增试卷
     */
    @PreAuthorize("@ss.hasPermi('course:paper:add')")
    @Log(title = "试卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPaper sysPaper)
    {
        //试卷名称唯一
        if (SysConstants.CONSTANT_STR1.equals(sysPaperService.checkPaperNameUnique(sysPaper)))
        {
            return AjaxResult.error("新增试卷：'" + sysPaper.getPaperName() + "'失败，名称已存在");
        }
        return toAjax(sysPaperService.insertSysPaper(sysPaper));
    }

    /**
     * 修改试卷
     */
    @PreAuthorize("@ss.hasPermi('course:paper:edit')")
    @Log(title = "试卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPaper sysPaper)
    {
        //试卷名称唯一
        if (SysConstants.CONSTANT_STR1.equals(sysPaperService.checkPaperNameUnique(sysPaper)))
        {
            return AjaxResult.error("新增试卷：'" + sysPaper.getPaperName() + "'失败，名称已存在");
        }
        return toAjax(sysPaperService.updateSysPaper(sysPaper));
    }

    /**
     * 删除试卷
     */
    @PreAuthorize("@ss.hasPermi('course:paper:remove')")
    @Log(title = "试卷", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[] paperIds)
    {
        return toAjax(sysPaperService.deleteSysPaperByPaperIds(paperIds));
    }
}
